<?php
namespace app\common\model;

use think\Model;
use hg\apidoc\annotation\Field;
use hg\apidoc\annotation\WithoutField;
use hg\apidoc\annotation\AddField;
use hg\apidoc\annotation\Param;

class Role extends Model
{
    protected $table = 'role';
    public function superUser()
    {
        return $this->belongsTo(SuperUser::class,'role_id','id');
    }
    /**
     * 保存新添加的角色
     */
    public function saveRole($data){
        if (empty($data) || !is_array($data)) return false;
        return $this->save($data);
    }
    /**
     * 更新角色
     */
    public function updateRole($data){
        if (empty($data) || !is_array($data)) return false;
        $where = [
            'id' => $data['id'],
        ];
        return $this->where($where)->update($data);
    }
    /**
     * 修改角色状态
     */
    public function statusRole($data){
        if (empty($data)) return false;
        $where = [
            'id' => $data['id'],
        ];
        return $this->where($where)->update(['status' => $data['status']]);
    }
    /**
     * 角色授权
     */
    public function authRole($data){
        if (empty($data)) return false;
        $where = [
            'id' => $data['id'],
        ];
        return $this->where($where)->update(['auths' => $data['auths']]);
    }
    /**
     * 获取角色列表
     * @WithoutField()
     */
    public function getRoleList($data, $super_user_auth)
    {
        if (empty($data)) return false;
        $where = [
            ['status' ,'>' , $super_user_auth['id'] === 1 ? -20000 : -1],
            ['rolename', 'like', '%' . $data['rolename'] . '%']
        ];
        $order = [
            'id'=>'asc'
        ];
        $result = [
            'list' => $this->where($where)->limit($data['pageRow'])->page($data['pageIndex'])->order($order)->select(),
            'count' => $this->where($where)->order($order)->count()
        ];
        return $result;
    }
    /**
     * 通过角色名查找角色
     */
    public function getRoleByRolename($rolename){
        if (empty($rolename)){
            return false;
        }
        return $this->where(['rolename'=>trim($rolename)])->find();
    }
}
